package com.ddwl.shop.service;

import com.ddwl.common.constant.RedisKey;
import com.ddwl.common.constant.ShopEnum;
import com.ddwl.common.exception.CommonException;
import com.ddwl.common.exception.ExceptionCode;
import com.ddwl.common.exception.ParamException;
import com.ddwl.common.feign.api.UserApi;
import com.ddwl.common.service.RedisService;
import com.ddwl.common.util.BeanConverUtils;
import com.ddwl.schema.bo.shop.ShopStaffBo;
import com.ddwl.schema.vo.shop.ShopStaffVo;
import com.ddwl.schema.vo.shop.ShopVo;
import com.ddwl.schema.vo.user.UserVo;
import com.ddwl.shop.dao.ShopDao;
import com.ddwl.shop.dao.ShopStaffDao;
import com.ddwl.shop.dao.model.Shop;
import com.ddwl.shop.dao.model.ShopStaff;
import io.gitee.tooleek.lock.spring.boot.annotation.Lock;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.concurrent.TimeUnit;


/**
 * 门店审核
 * @author wenbin
 * @version 1.0.0.RELEASE
 * @date 2020/3/5 11:00 上午
 */
@Service
@RequiredArgsConstructor
public class ShopStaffService {

    private final ShopStaffDao shopStaffDao;
    private final RedisService redisService;
    private final ShopDao shopDao;
    private final UserApi userApi;

    public String createShareKey(String userId,String shareKey) {
        String key = RedisKey.WX_SHARE_KEY.getKey(shareKey);
        String shopId=shopDao.getShopByUserId(userId).getId();
        redisService.getValueOps().set(key,shopId,5, TimeUnit.MINUTES);
        return key;
    }

    public String checkShareKey(String shareKey) {
        String shopId = redisService.getValueOps().get(RedisKey.WX_SHARE_KEY.getKey(shareKey));
        ParamException.isBlank (shopId, ExceptionCode.WX_SHARE_KEY_NOT_EXISTS.getMsg());
        return shopId;
    }

    @Transactional(rollbackFor = Throwable.class)
    @Lock(leaseTime=3)
    public void create(ShopStaffBo bo,String pk) {
        ParamException.isBlank(bo.getOpenId(),"openId为空");
        ParamException.isBlank(bo.getShareKey(),"分享key为空");
        UserVo userVo = userApi.getByOpenId(bo.getOpenId(), pk).getResult();
        if(userVo == null) {
            throw new CommonException(ExceptionCode.DATA_NOT_EXISTS);
        }
        String shopId = checkShareKey(bo.getShareKey());
        bo.setShopId(shopId);
        //重复校验
        ShopStaff queryStaff=new ShopStaff();
        queryStaff.setUserId(userVo.getId());
        queryStaff.setShopId(shopId);
        ShopStaff staff = shopStaffDao.selectOne(queryStaff);
        if (null==staff){
            ShopStaff shopStaff=BeanConverUtils.convertBean(bo,ShopStaff.class);
            Shop shop = shopDao.queryShop(shopId);
            if(shop.getUserId().equals(queryStaff.getUserId())) {
                throw new CommonException("您已经是店主,无需申请");
            }
            shopStaff.setShopName(shop.getName());
            shopStaff.setUserTel(userVo.getMobile());
            shopStaff.setUserId(userVo.getId());
            shopStaffDao.insert(shopStaff);
            redisService.getValueOps().getOperations().delete(RedisKey.WX_SHARE_KEY.getKey(bo.getShareKey()));
        }
    }

    public void delete(String id) {
        shopStaffDao.deleteById(id);
    }

    @Transactional(rollbackFor = Throwable.class)
    public void update(ShopStaffBo bo) {
        ParamException.isBlank(bo.getId(),"分享id为空");
        ShopStaff shopStaff=BeanConverUtils.convertBean(bo,ShopStaff.class);
        shopStaffDao.updateByPrimaryKeySelective(shopStaff);
    }

    public List<ShopStaffVo> list(String userId) {
        ParamException.isBlank(userId,"用户id为空");
        ShopStaff shopStaff=new ShopStaff();
        Shop shop = shopDao.getShopByUserId(userId);
        if(shop!=null){
            String shopId=shop.getId();
            shopStaff.setShopId(shopId);
            return BeanConverUtils.convertBeanList(shopStaffDao.select(shopStaff),ShopStaffVo.class);
        }
        return null;
    }

    public List<ShopStaffVo> getShopList(String userId) {
        ShopStaff shopStaff=new ShopStaff();
        shopStaff.setUserId(userId);
        List<ShopStaffVo> shopStaffVos = BeanConverUtils.convertBeanList(shopStaffDao.select(shopStaff), ShopStaffVo.class);
        shopStaffVos.forEach(o->o.setIsOwnShop(ShopEnum.OwnShopEnum.NO.getCode()));
        //查询店主店铺
        ShopVo shopVo = BeanConverUtils.convertBean(shopDao.getShopByUserId(userId), ShopVo.class);
        if (shopVo!=null){
            ShopStaffVo vo=new ShopStaffVo();
            vo.setShopId(shopVo.getId());
            vo.setShopName(shopVo.getName());
            vo.setIsOwnShop(ShopEnum.OwnShopEnum.YES.getCode());
            vo.setStatus(shopVo.getStatus());
            shopStaffVos.add(vo);
        }
        return shopStaffVos;
    }
}
